//
//  MyBidJobDetailView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/17.
//

import UIKit
import SnapKit
class ServiceDetailView: ServiceBaseView {
    //
    var starJobClosure: (()->Void)?
    var finishJobClosure: (()->Void)?
    var clickTabHistoryClosure: (()->Void)?
    var scrollView: UIScrollView!
    var topBarView: MyBidJobDetailTopBar!
    var tabDetailView: ServiceDetailTabDetail!
    var tabChecklistView: ServiceDetailTabChecklist!
    var tabHistoryView: ServiceDetailTabHistory!
    //
    var operateBtn = OperationButton("Set Off")
    //加载失败的额视图
    lazy var loadFaildView = DatalLoadFaildView()
    
    override func setUpviews() {
        //加载失败的试图
        loadFaildView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(loadFaildView)
        loadFaildView.snp.makeConstraints { make in
            make.top.equalTo(self.topBackView.snp.bottom)
            make.bottom.left.right.equalToSuperview()
        }
        //1.top tab bar
        topBarView = MyBidJobDetailTopBar()
        self.addSubview(topBarView)
        self.topBarView.snp.makeConstraints { (make) in
            make.top.equalTo(self.topBackView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(55)
        }
        //2.底部的操作按钮，set off or finish job or 不显示
        self.addSubview(operateBtn)
        operateBtn.snp.makeConstraints { make in
            make.top.equalTo(self.snp.bottom)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(OperationButton.height)
        }
        //3.tab detail
        tabDetailView = ServiceDetailTabDetail()
        self.addSubview(tabDetailView)
        tabDetailView.snp.makeConstraints { (make) in
            make.top.equalTo(self.topBarView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(self.operateBtn.snp.top)
        }
        //4.tab checklist
        tabChecklistView = ServiceDetailTabChecklist()
        self.addSubview(tabChecklistView)
        tabChecklistView.snp.makeConstraints { (make) in
            make.top.equalTo(self.topBarView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(self.operateBtn.snp.top)
        }
        //5.tab history info
        tabHistoryView = ServiceDetailTabHistory()
        self.addSubview(tabHistoryView)
        tabHistoryView.snp.makeConstraints { (make) in
            make.top.equalTo(self.topBarView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(self.operateBtn.snp.top)
        }
        self.topBarView.detailBtn.addTarget(self, action: #selector(changeTab(_:)), for: UIControl.Event.touchUpInside)
        self.topBarView.checklistBtn.addTarget(self, action: #selector(changeTab(_:)), for: UIControl.Event.touchUpInside)
        self.topBarView.historyBtn.addTarget(self, action: #selector(changeTab(_:)), for: UIControl.Event.touchUpInside)
        //
        self.operateBtn.addTarget(self, action: #selector(startOrFinishJobClick), for: UIControl.Event.touchUpInside)
        //
        self.hideAllView()
    }
    //MARK: - 初始化hide all views
    func hideAllView(){
        self.topBarView.isHidden = true
        self.tabDetailView.isHidden = true
        self.tabChecklistView.isHidden = true
        self.tabHistoryView.isHidden = true
        self.loadFaildView.isHidden = true
    }
    //MARK: - 加载成功显示detail viewe
    func showDetailView(){
        self.topBarView.isHidden = false
        self.tabDetailView.isHidden = false
        self.tabChecklistView.isHidden = false
        self.tabHistoryView.isHidden = false
        self.operateBtn.isHidden = false
        self.loadFaildView.isHidden = true
    }
    //MARK: - 加载失败显示faild view
    func showFaildView(){
        self.topBarView.isHidden = true
        self.tabDetailView.isHidden = true
        self.tabChecklistView.isHidden = true
        self.tabHistoryView.isHidden = true
        self.loadFaildView.isHidden = false
        self.operateBtn.isHidden = true
    }
    //MARK: - change tab
    @objc func changeTab(_ sender: UIButton){
        self.topBarView.changeImage(sender.tag)
        if sender.tag == 0 {
            self.tabDetailView.isHidden = false
            self.tabChecklistView.isHidden = true
            self.tabHistoryView.isHidden = true
            self.bringSubviewToFront(self.tabDetailView)
        }else if sender.tag == 1 {
            self.tabDetailView.isHidden = true
            self.tabChecklistView.isHidden = false
            self.tabHistoryView.isHidden = true
            self.bringSubviewToFront(tabChecklistView)
        }else if sender.tag == 2 {
            self.tabDetailView.isHidden = true
            self.tabChecklistView.isHidden = true
            self.tabHistoryView.isHidden = false
            self.bringSubviewToFront(tabHistoryView)
            self.clickTabHistoryClosure?()
        }
    }
    //MARK: - 底部的操作按钮
    func setBottomBtnsByStatus(_ model: ServiceDetailModel){
        operateBtn.snp.remakeConstraints { make in
            make.top.equalTo(self.snp.bottom)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(OperationButton.height)
        }
        if APPConfig.loginInfo.fullName == model.installer_name {// only current user who is the installer can operate job
            if model.status == ServiceJobStatusName.scheduled.rawValue {
                operateBtn.snp.remakeConstraints { make in
                    make.top.equalTo(self.safeAreaLayoutGuide.snp.bottom).offset(-(OperationButton.height + 10))
                    make.left.equalTo(16)
                    make.right.equalTo(-16)
                    make.height.equalTo(OperationButton.height)
                }
                self.operateBtn.tag = 1
                self.operateBtn.setTitle("Set Off", for: UIControl.State.normal)
                
            }else if model.status == ServiceJobStatusName.started.rawValue || model.status == ServiceJobStatusName.unqualified.rawValue{
                operateBtn.snp.remakeConstraints { make in
                    make.top.equalTo(self.safeAreaLayoutGuide.snp.bottom).offset(-(OperationButton.height + 10))
                    make.left.equalTo(16)
                    make.right.equalTo(-16)
                    make.height.equalTo(OperationButton.height)
                }
                self.operateBtn.tag = 2
                self.operateBtn.setTitle("Finish", for: UIControl.State.normal)
            }
        }
    }
    //
    @objc func startOrFinishJobClick(_ sender: OperationButton){
        if sender.tag == 1{
            self.starJobClosure?()
        }else if sender.tag == 2 {
            self.finishJobClosure?()
        }
    }
}
